// miniprogram/pages/chart/billedit.js
var Util = require('../../utils/util.js')
const db = wx.cloud.database()
const _ = db.command
Page({
  data: {

  },

  //跳转选择分类页面
  toCategoryChoosePage(){
    wx.navigateTo({
      url: '../home/categoryChoose?id=' + this.data.cateId
    })
  },

  //跳转选择账户页面
  toAssetChoosePage(e){
    var assetId = e.currentTarget.dataset.assetId;
    wx.navigateTo({
      url: '../home/assetChoose?id=' + assetId
    })
  },

  //选择本地照片
  chooseImage:function(event){
    var imgArr = this.data.chooseFiles;
    var leftCount = 3 - imgArr.length;

    if (leftCount <= 0){
      return;
    }

    wx.chooseImage({
      count: leftCount,
      sourceType: ['album', 'camera'],
      success: (result) => {
        this.setData({
          chooseFiles:imgArr.concat(result.tempFilePaths)
        })
      }
    })
  },

  //删除已经选择的图片
  deleteImage:function (event) {
    var index = event.currentTarget.dataset.idx,
        that=this;
    that.setData({
      deleteIndex:index
    });
    that.data.chooseFiles.splice(index,1);
    setTimeout(function () {
      that.setData({
        deleteIndex:-1,
        chooseFiles:that.data.chooseFiles
      });
    },500)
  },

  bindDateChange: function(e) {
    this.setData({
      datetime: e.detail.value
    })
  },

  formSubmit: function(e) {
    console.log(e.detail.value)
    wx.cloud.callFunction({
      name: 'update_payment',
      data: {
        id: this.data.id,
        type: e.detail.value.type,
        amount: e.detail.value.amount,
        account: e.detail.value.account,
        account1: e.detail.value.account1,
        account2: e.detail.value.account2,
        datetime: e.detail.value.datetime,
        desc: e.detail.value.desc
      }
    }).then(res => {
      console.log(res)
      wx.showToast({
        title: '修改成功',
      })
      db.collection('user_bill').where({
        _id: this.data.id
      }).get().then(res => {
        console.log(res.data[0].imgs)
        this.setData({
          chooseFiles: res.data[0].imgs
        })
      })
      this.setData({
        amount: e.detail.value.amount,
        account: e.detail.value.account,
        account1: e.detail.value.account1,
        account2: e.detail.value.account2,
        datetime: e.detail.value.datetime,
        desc: e.detail.value.desc,
      })
      var prevPage = getCurrentPages()[getCurrentPages().length - 2];
      prevPage.setData({
        amount: e.detail.value.amount,
        account: e.detail.value.account,
        account1: e.detail.value.account1,
        account2: e.detail.value.account2,
        datetime: e.detail.value.datetime,
        desc: e.detail.value.desc,
        chooseFiles: this.data.chooseFiles
      })
    })
  },

  onLoad: function(options) {
    this.setData({
      id: options.id,
      openid: options.openid,
      desc: options.desc,
      type: options.type,
      datetime: options.datetime,
      amount: options.amount
    })
    if (this.data.type == '支出') {
      this.setData({
        choosed_cate1: options.cate,
        choosed_asset1: options.account,
        cateId: 0
      })
    }
    else if (this.data.type == '收入') {
      this.setData({
        choosed_cate2: options.cate,
        choosed_asset2: options.account,
        cateId: 1
      })
    }
    else if (this.data.type == '转账') {
      this.setData({
        choosed_asset1: options.account1,
        choosed_asset2: options.account2,
        cateId: 2
      })
    }
    else if (this.data.type == '还款') {
      this.setData({
        choosed_asset1: options.account1,
        choosed_asset2: options.account2,
        cateId: 3
      })
    }
    
    db.collection('user_bill').where({
      _id: this.data.id
    }).get().then(res => {
      console.log(res.data[0].imgs)
      this.setData({
        chooseFiles: res.data[0].imgs
      })
    })
  }
})